/**
 * @filename:StkOtherInController Feb 5, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.PrdFeed;
import com.wonder.app.request.PrdFeedRequest;
import com.wonder.app.service.PrdFeedService;
import com.wonder.framework.authorization.CheckAdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import javax.servlet.http.HttpServletRequest;
@Api(tags = "生产补料单")
@RestController
@RequestMapping("/app/PrdFeed")
public class PrdFeedController {

@Autowired
private PrdFeedService prdFeedService;


    @Autowired
    private CheckAdminToken checkAdminToken;

    @Autowired
    private SysAdminService adminService;

    @Autowired
    @Qualifier("sqlServerJdbcTemplate")
    private JdbcTemplate sqlServerJdbcTemplate;

    private Map<Long,Long> lastRequestTimeMap = new ConcurrentHashMap<>();

    @ApiOperation(value = "生产补料单列表")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public Map<String,Object> index(@RequestBody PrdFeedRequest prdFeedRequest) {
        Page<PrdFeed> page = new Page<PrdFeed>(prdFeedRequest.getPage(), prdFeedRequest.getLimit());
        IPage<PrdFeed> list = prdFeedService.selectList(prdFeedRequest, page);
        Map<String,Object> retMap=new HashMap<>();
        retMap.put("data", CommonPage.restPage(list));
        return AjaxMap.success(retMap);
    }
    /**
     * @explain 查看对象明细
     * @param   map 对象参数：id
     * @return  JsonResult
     * @author  Wonder
     */
    @ApiOperation(value = "查询prdfeed明细")
    @ResponseBody
    @PostMapping(value = "/prdfeeddetail")

    public AjaxResult<Map<String,Object>> getPrdFeedDetail(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        if (null == map){
            return AjaxResult.failed("system.parameterError");
        }
        Map<String,Object> data = prdFeedService.getPrdFeedDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }


    /**
     * 创建补料单
     * @param map
     * @return
     */
    @ApiOperation(value = "创建补料单")
    @ResponseBody
    @PostMapping(value = "/create_feed_bill")
    public AjaxResult<Boolean> createFeedBill(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();
        //从map中查找上次请求的时间戳
        Long lastRequestTime = lastRequestTimeMap.get(userId);
        //如果为空或者上次请求的时间戳与当前时间做差大于5s，则视为新请求，否则重复请求。
        if (lastRequestTime == null || System.currentTimeMillis() - lastRequestTime > 20000) {
            lastRequestTimeMap.put(userId, System.currentTimeMillis());
            // 处理请求
            Boolean res = prdFeedService.createFeedBill(userId,userName,map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        } else {
            // 重复请求
            throw new Exception("正在补料，请不要重复点击");
        }


    }


    /**
     * 删除补料中的临时数据
     * @param map
     * @return
     */
    @ApiOperation(value = "删除补料中的临时数据")
    @ResponseBody
    @PostMapping(value = "/delete_feed_detail_temp")
    public AjaxResult<?> deleteFeedDetailTemp(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();

        // 处理请求
        Boolean res = prdFeedService.deleteFeedDetailTemp(userName,map);

        return AjaxResult.success(res);

    }


    /**
     * 添加补料临时数据
     * @param map
     * @return
     */
    @ApiOperation(value = "添加补料临时数据")
    @ResponseBody
    @PostMapping(value = "/create_feed_temp")
    public AjaxResult<?> createFeedTemp(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception{
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long userId = user.getUser_id();
        String userName = user.getName();

        // 处理请求
        Boolean res = prdFeedService.createFeedTemp(userName,map);

        return AjaxResult.success(res);

    }

    @PostMapping("/sqlserver-users")
    public List<Map<String, Object>> getSqlServerUsers() {
        return sqlServerJdbcTemplate.queryForList("SELECT top 10 * FROM dbo.T_BD_MATERIAL;");
    }

}
